package fast.mvc;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import indzcraft.loader.Item;
import indzcraft.loader.Slot;
import indzcraft.ranking.EP;

public class ItemPanel extends JPanel {
	
	private static final long serialVersionUID = 852930853916977746L;
	private int selected = 0;
	private Slot slot;
	HashMap<JButton,Item> items = new HashMap<JButton,Item>();
	
	public ItemPanel(Slot s){
		this.slot = s;
		this.setLayout(null);
	}
	
	public void showSlot(){
		System.out.println("SlotName: "+  slot.getName());
	}

	public HashMap<JButton,Item> getItems(){
		return items;
	}
	
	public void addItemChangeListener(JButton i,ActionListener l){
        i.addActionListener(l);
    }

	public void showItems(Iterator<EP<Item>> it, Item curItem) {
		int count = 0;
		double max = 0;
		while (it.hasNext()) {
			Item item = it.next().getItem();
			double ep = 0.0;
			if(item != null)
				ep = item.getEp();
			if(max == 0) max = ep;
			boolean equiped = false;
			if (item == curItem)
				equiped = true;
			
			String name = "None";
			String icon = "inv_misc_questionmark";
			if(item != null){
				name = item.getName();
				icon = item.getIcon();
			}
			JPanel itemContainer = new JPanel(null);
			itemContainer.setBounds(0, count*55, 320, 55);
			if(equiped){
				itemContainer.setBackground(Color.decode("#D1D0D0"));
				selected = count;
			}else
				itemContainer.setBackground(null);
			this.add(itemContainer);
			
			JButton btnItem = new JButton();
			if(equiped)
				btnItem.setBorder(new LineBorder(Color.GREEN,2));
			else
				btnItem.setBorder(new LineBorder(Color.BLACK,1));
			btnItem.setBounds(0, 0, 54, 54);

			ChangeIconTask task = new ChangeIconTask(btnItem,icon);
			task.execute();

			items.put(btnItem,item);
			itemContainer.add(btnItem);
			
			JLabel labelName = new JLabel(name + " (" + item.getItemLevel() + ")");
			if(item.getQuality() == 3)
				labelName.setForeground(Color.decode("#0080ff"));
			if(item.getQuality() == 4)
				labelName.setForeground(Color.decode("#b048f8"));
			if(item.getQuality() == 5)
				labelName.setForeground(Color.decode("#ff8000"));
			labelName.setBounds(55, 0, 250, 20);
			itemContainer.add(labelName);
			
			String speed = "";
			if(item.getSpeed() != 0.0){
				speed += item.getSpeed();
			}
			String gearBuff = "";
			if(item.getGearBuff() != null){
				gearBuff = item.getGearBuff();
			}
			String info = item.getWeaponTypeName() + " " + speed + " " + gearBuff;
			info += item.getStatString();
			JLabel labelInfo = new JLabel(info);
			labelInfo.setBounds(55, 20, 220, 20);
			itemContainer.add(labelInfo);
			
			double diff;
            if (curItem != null)
                diff = ep - curItem.getEp();
            else
                diff = ep;
            JLabel label = new JLabel(String.format("%+.2f", diff));
            label.setBounds(155, 20,160,20);
            label.setHorizontalAlignment(JLabel.RIGHT);
			itemContainer.add(label);
			
			RatingPanel rp = new RatingPanel(ep, max);
			rp.setLocation(55, 40);
			itemContainer.add(rp);
			
			count++;
		}
		this.setPreferredSize(new Dimension(300,1+count*55));
		this.setSize(new Dimension(300,1+count*55));
	}

	public int getSelectedPosition() {
		if(selected >= 3)
			selected -= 3;
		else
			selected = 0;
		return selected * 55;
	}
}
